<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/login.css">
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
    
<body>
    <div class="nav">
        <div class="left">
            <img src="./asset/img/logo.png" alt="" class="via">
            <span>KOKYU博客</span>
        </div>
    </div>
    <div class="container">
        <div class="login-dialog">
            <h3>登录博客</h3>
            <div class="tips" id="tips" >提示消息</div>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="jump">
                <div style="width: 50px"></div>
                <a style="font-size: 14px; width: 400px" id="href" href="./register.html">没有账号？点击注册</a>
            </div>
            <div class="row">
                <input type="submit" id="login" value="登录">
            </div>
        </div>
    </div>




    <script>
        function tips(msg) {
            const tipsBox = document.querySelector('#tips')
            tipsBox.classList.add('show')
            tipsBox.innerHTML = msg
            setTimeout(() => {
                tipsBox.classList.remove('show')
            }, 3000)
        }


        document.querySelector('#login').addEventListener('click',()=>{
            const username = document.querySelector('#username').value.trim()
            const password = document.querySelector('#password').value.trim()
            axios({
                url:'http://47.98.20.202:8000/user/login/',
                method:'post',
                data:{
                    username,
                    password
                }
            }).then(res => {
                //tips(res.data.message)
                if(res.data.code === 0){
                    localStorage.setItem('token',res.data.data.userId);
                    // console.log(localStorage.getItem('token'))
                    window.location.href = "home.html";
                }else{
                    tips("用户名或密码错误")
                }
            }).catch(error => {
                console.error('Error:', error);
            })
        })
        function goToRegister() {
            window.location.href = "register.html";
        }
    </script>
</body>
<script src="js/jquery.min.js"></script>
<script src="js/login.js"></script>
</html>